<?php
namespace zzcms\primary\controller;


class Product extends Base
{
    protected $model;

    protected function initialize()
    {
        parent::initialize();
        $this->model = new \zzcms\admin\model\Product();
    }

    public function index()
    {
        $where = ['status' => 1];
        //品牌ID
        $bid = $this->request->has('bid') ? $this->request->param('bid', 0, 'intval') : 0;
        //类型ID
        $mid = $this->request->has('mid') ? $this->request->param('mid', 0, 'intval') : 0;
        //网络
        $nid = $this->request->has('nid') ? $this->request->param('nid', 0, 'intval') : 0;
        //收款方式
        $pid = $this->request->has('pid') ? $this->request->param('pid', 0, 'intval') : 0;
        //POS机颜色
        $cid = $this->request->has('cid') ? $this->request->param('cid') : '';
        //特色功能
        $fid = $this->request->has('fid') ? $this->request->param('fid') : '';
        $lists = $this->model->where($where)->order('listorder desc,id desc')->select();
        foreach ($lists as $k => $item) {
            if (empty($item->thumb)) {
                $item->thumb = '';
            } else {
                $thumbs = explode(',', trim($item->thumb, ','));
                $item->thumb = reset($thumbs);
            }
        }
        $info = [];
        $info['mid'] = $mid;
        $info['bid'] = $bid;
        $info['nid'] = $nid;
        $info['pid'] = $pid;
        $info['cid'] = $cid;
        $info['fid'] = $fid;
        //品牌列表
        $info['brand_lists'] = getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc']);
        //类型
        $info['module_lists'] = config('zzcms.product_module');
        //网络
        $info['network_lists'] = config('zzcms.product_network');
        //收款方式
        $info['payment_lists'] = config('zzcms.product_payment');
        //颜色
        $info['color_lists'] = config('zzcms.product_color');
        //特色功能
        $info['function_lists'] = config('zzcms.product_function');
        $info['seo'] = $this->seo(['name' => 'product']);
        $this->assign('lists', $lists);
        $this->assign($info);
        return $this->fetch();
    }

    public function lists()
    {
        $where = [];
        $page = isset($this->info['page']) ? max(intval($this->info['page']), 1) : 1;
        $pagesize = isset($this->info['pagesize']) ? intval($this->info['pagesize']) : 10;
        $bid = isset($this->info['bid']) ? intval($this->info['bid']) : 0;
        if (!empty($bid)) {
            if (false === strpos($bid, ',')) {
                $where[] = ['brand_id', 'eq', $bid];
            } else {
                $bid_array = explode(',', $bid);
                foreach ($bid_array as $k => $item) {
                    if (empty($item)) {
                        unset($bid_array[$k]);
                    }
                }
                if ($bid_array) {
                    $where[] = ['brand_id', 'in', $bid];
                }
            }
        }
        //类型
        $mid = isset($this->info['mid']) ? intval($this->info['mid']) : 0;
        if (!empty($mid)) {
            if (false === strpos($mid, ',')) {
                $where[] = ['module', 'eq', $mid];
            } else {
                $mid_array = explode(',', $mid);
                foreach ($mid_array as $k => $item) {
                    if (empty($item)) {
                        unset($mid_array[$k]);
                    }
                }
                if ($mid_array) {
                    $where[] = ['module', 'in', $mid];
                }
            }
        }

        //网络
        $nid = isset($this->info['nid']) ? $this->info['nid'] : 0;
        if (!empty($nid)) {
            if (false === strpos($nid, ',')) {
                $where[] = ['network', 'like', '%' . $nid . '%'];
            } else {
                $nid_array = explode(',', $nid);
                foreach ($nid_array as $k => $item) {
                    if (empty($item)) {
                        unset($nid_array[$k]);
                        continue;
                    }
                    $nid_array[$k] = '%' . $item . '%';
                }
                if ($nid_array) {
                    $where[] = ['network', 'like', $nid_array, 'or'];
                }
            }
        }
        //支付方式
        $pid = isset($this->info['pid']) ? intval($this->info['pid']) : 0;
        if (!empty($pid)) {
            if (false === strpos($pid, ',')) {
                $where[] = ['payment', 'like', '%' . $pid . '%'];
            } else {
                $pid_array = explode(',', $pid);
                foreach ($pid_array as $k => $item) {
                    if (empty($item)) {
                        unset($pid_array[$k]);
                        continue;
                    } else {
                        $pid_array[$k] = '%' . $item . '%';
                    }
                }
                if ($pid_array) {
                    $where[] = ['payment', 'like', $pid_array, 'or'];
                }
            }
        }

        //POS机颜色
        $cid = isset($this->info['cid']) ? intval($this->info['cid']) : '';
        if ($cid) {
            if (false === strpos($cid, ',')) {
                $where[] = ['pos_color', 'like', '%' . $cid . '%'];
            } else {
                $cid_array = explode(',', $cid);
                foreach ($cid_array as $k => $item) {
                    if (empty($item)) {
                        unset($cid_array[$k]);
                        continue;
                    } else {
                        $cid_array[$k] = '%' . $item . '%';
                    }
                }
                if ($cid_array) {
                    $where[] = ['pos_color', 'like', $cid_array, 'or'];
                }
            }
        }

        //特色功能
        $fid = isset($this->info['fid']) ? intval($this->info['fid']) : '';
        if ($fid) {
            if (false === strpos($fid, ',')) {
                $where[] = ['function', 'like', '%' . $fid . '%'];
            } else {
                $fid_array = explode(',', $fid);
                foreach ($fid_array as $k => $item) {
                    if (empty($item)) {
                        unset($fid_array[$k]);
                        continue;
                    } else {
                        $fid_array[$k] = '%' . $item . '%';
                    }
                }
                if ($fid_array) {
                    $where[] = ['function', 'like', $fid_array, 'or'];
                }
            }
        }


        $keyword = isset($this->info['keyword']) ? $this->info['keyword'] : '';
        if (!empty($keyword)) {
            $where[] = ['name', 'like', '%' . $keyword . '%'];
        }

        $count = $this->model->where($where)->count();
        $lists = $this->model->where($where)->page($page, $pagesize)->select();
        $brands = getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc']);
        //POS机类型
        $product_modules = config('zzcms.product_module');
        //POS机网络
        $product_networks = config('zzcms.product_network');
        //POS机收款方式
        $product_payments = config('zzcms.product_payment');
        //POS机颜色
        $product_colors = config('zzcms.product_color');
        //POS机特色功能
        $product_functions = config('zzcms.product_function');
        foreach ($lists as $k => $item) {
            $item['product_module'] = $product_modules[$item['module']];
            $item['product_network'] = str_replace(array_keys($product_networks), $product_networks, $item['network']);
            $item['product_payment'] = str_replace(array_keys($product_payments), $product_payments, $item['payment']);
            $item['product_color'] = str_replace(array_keys($product_colors), $product_colors, $item['pos_color']);
            $item['product_function'] = str_replace(array_keys($product_functions), $product_functions, $item['function']);
            $brand_info = isset($brands[$item['brand_id']]) ? $brands[$item['brand_id']] : [];
            if (empty($brand_info)) {
                $item['brand_name'] = '';
                $item['brand_image'] = '';
            } else {
                $item['brand_name'] = $brand_info['name'];
                $item['brand_image'] = false === strpos($brand_info['thumb'], 'http') ? $this->base_image_url . $brand_info['thumb'] : $brand_info['thumb'] . '?x-oss-process=image/resize,m_fill,h_50,w_50';
            }
            $images = empty($item['thumb']) ? [] : explode(',', $item['thumb']);
            if ($images) {
                foreach ($images as $kk => $image_thumb) {
                    $image_thumb = $image_thumb . '?x-oss-process=image/resize,m_lfit,w_110';
                    $images[$kk] = $image_thumb;
                }
            }
            $item['images'] = $images;
            $item['images'] = empty($item['thumb']) ? [] : explode(',', $item['thumb']);
            $item['image'] = empty($item['thumb']) ? '/static/images/dingdan_03.png' : $item['images'][0];
            //图片数量
            $item['image_count'] = count($item['images']);
            //最多显示几张图片
            $item['show_count'] = 3;
            $item['third_img'] = $item['image_count'] > $item['show_count'] ? $item['images'][2] : '';
            $item['hide_count'] = $item['image_count'] > $item['show_count'] ? $item['image_count'] - $item['show_count'] : 0;
            $lists[$k] = $item;
        }
        $this->array2json(['code' => 0, 'data' => $lists, 'count' => $count, 'sql' => $this->model->getLastSql()]);
    }

    public function detail()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id < 1) {
            $id = 1;
            //return $this->error('参数非法', url('home/product/index'));
        }
        $info = $this->model->getDetail($id);
        if (empty($info)) {
            return $this->error('信息不存在', url('home/product/index'));
        }
        $brands = getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc']);
        $companys = getCache('company');
        $brand = isset($brands[$info['brand_id']]) ? $brands[$info['brand_id']] : [];
        $info['company'] = isset($companys[$brand['company_id']]) ? $companys[$brand['company_id']] : [];
        $info['brand'] = $brand;
        $info['brands'] = $brands;
        //点击量+1
        $this->model->where(['id' => $id])->setInc('hits');
        //POS机类型
        $product_modules = config('zzcms.product_module');
        $info['product_module'] = isset($product_modules[$info['module']]) ? $product_modules[$info['module']] : '';
        //POS机网络
        $product_networks = config('zzcms.product_network');
        //POS机收款方式
        $product_payments = config('zzcms.product_payment');
        //POS机颜色
        $product_colors = config('zzcms.product_color');
        //POS机特色功能
        $product_functions = config('zzcms.product_function');
        $info['product_network'] = str_replace(array_keys($product_networks), $product_networks, $info['network']);
        $info['product_payment'] = str_replace(array_keys($product_payments), $product_payments, $info['payment']);
        $info['product_color'] = str_replace(array_keys($product_colors), $product_colors, $info['pos_color']);
        $info['product_function'] = str_replace(array_keys($product_functions), $product_functions, $info['function']);
        $info['thumbs'] = explode(',', trim($info['thumb'], ','));
        $info['brand_name'] = isset($brand['name']) ? $brand['name'] : '';
        //公司信息
        $this->array2json(['code' => 0, 'msg' => 'ok', 'data' => $info]);
    }

    /**
     * 获取产品的品牌名
     */
    public function brands()
    {
        $brands = getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc']);
        $array = [];
        foreach ($brands as $item) {
            if ($item['status'] != 1) {
                continue;
            }
            $array[] = ['id' => $item['id'], 'name' => $item['name']];
        }
        $this->array2json(['code' => 0, 'data' => $array]);
    }

    /**
     * 获取产品的品牌名
     */
    public function productSelect()
    {
        //品牌
        $brands = getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc']);
        $array = [];
        foreach ($brands as $item) {
            if ($item['status'] != 1) {
                continue;
            }
            $array[] = ['id' => $item['id'], 'name' => $item['name'], 'active' => false];
        }
        $info = [];
        $info['brand'] = $array;
        //类型
        $modules = config('zzcms.product_module');
        $array = [];
        foreach ($modules as $k => $item) {
            $array[] = ['id' => $k, 'name' => $item, 'active' => false];
        }
        $info['module'] = $array;
        //网络
        $networks = config('zzcms.product_network');
        $array = [];
        foreach ($networks as $k => $item) {
            $array[] = ['id' => $k, 'name' => $item, 'active' => false];
        }
        $info['network'] = $array;
        //收款方式
        $payments = config('zzcms.product_payment');
        $array = [];
        foreach ($payments as $k => $item) {
            $array[] = ['id' => $k, 'name' => $item, 'active' => false];
        }
        $info['payment'] = $array;
        //颜色
        $colors = config('zzcms.product_color');
        $array = [];
        foreach ($colors as $k => $item) {
            $array[] = ['id' => $k, 'name' => $item, 'active' => false];
        }
        $info['color'] = $array;
        //特色功能
        $functions = config('zzcms.product_function');
        $array = [];
        foreach ($functions as $k => $item) {
            $array[] = ['id' => $k, 'name' => $item, 'active' => false];
        }
        $info['functions'] = $array;
        $this->array2json(['code' => 0, 'data' => $info]);
    }


    /**
     * 产品切换标签
     */
    public function tabNar()
    {
        $array = [];
        $array['brand'] = [
            'name' => '品牌',
            'item' => getCache('brand', '', ['where' => ['status' => 1], 'order' => 'listorder desc,id desc'])
        ];
        $array['networks'] = [
            'name' => '网络',
            'item' => config('zzcms.product_network')
        ];
        $array['colors'] = [
            'name' => '颜色',
            'item' => config('zzcms.product_color')
        ];

        $array['functions'] = [
            'name' => '特色功能',
            'item' => config('zzcms.product_function')
        ];

        $array['payments'] = [
            'name' => '收款方式',
            'item' => config('zzcms.product_payment')
        ];
        $array['modules'] = [
            'name' => '类型',
            'item' => config('zzcms.product_module')
        ];
        $this->array2json(['code' => 0, 'data' => $array]);
    }
}
